﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WebCamDeskLib;

namespace WebCamTest
{
    public partial class Form1 : Form
    {
        private WebCamList fWebCamList;
        private Boolean fRandomMode = false;
        private Boolean fCanLoadPicture = true;
        private PictureManager fPictureManager = null;


        public Form1()
        {
            InitializeComponent();
            
        }
        

        private void LoadPicture(string aUrl, string aLocalPic)
        {
            if (fCanLoadPicture)
            {

                fCanLoadPicture = false;
                buRefresh.Enabled = false;
                Downloader myDownloader = new Downloader();
                myDownloader.RaiseDownloadCompleteEvent += new EventHandler<DownloadEventArgs>(myDownloader_RaiseDownloadCompleteEvent);
                myDownloader.RaiseDownloadPercentEvent += new EventHandler<DownloadProgressEventArgs>(myDownloader_RaiseDownloadPercentEvent);
                myDownloader.DownloadFile(aUrl, aLocalPic);
            }
        }

        private void RefreshPicture()
        {
            WebCam SelectedWebCam = fWebCamList.GetWebCamForDesc(cbWebCamList.Text);
            if (SelectedWebCam != null)
            {
                LoadPicture(SelectedWebCam.Adress, fPictureManager.GetNextFileName());
                cbInterval.Text = (SelectedWebCam.Refresh * 60 * 100).ToString();
                laAdress.Text = SelectedWebCam.Adress;
            }
        }
        void myDownloader_RaiseDownloadPercentEvent(object sender, DownloadProgressEventArgs e)
        {
            DlProgressBar.Value = (int)e.Percent;
        }

        void myDownloader_RaiseDownloadCompleteEvent(object sender, DownloadEventArgs e)
        {
            try
            {
                try
                {
                    pictureBox1.Load(e.Message);
                }
                catch
                {
                    MessageBox.Show(String.Format("Bild konnte nicht geladen werden: {0}", e.Message));
                }
            }
            finally
            {
                buRefresh.Enabled = true;
                DlProgressBar.Value = 0;
                fCanLoadPicture = true;
            }
            
        }
        private void Form1_Load(object sender, EventArgs e)
        {

            // Filename-Verwaltung
            fPictureManager = new PictureManager(@"c:\WebCamPix", "WCPic.jpg", 0);

            // Liste der verfübaren Cams
            fWebCamList = new WebCamList("Setup.ini");
            foreach (WebCam Cam in fWebCamList.WebCams)
            {
                cbWebCamList.Items.Add(Cam);
            }
            cbWebCamList.Text = cbWebCamList.Items[0].ToString();
            
            cbWebCamList.DropDownClosed +=new EventHandler(EhSelectWebCam);
            RefreshPicture();
        }

        private void buChangeAutoRefresh_Click(object sender, EventArgs e)
        {
            timer1.Enabled = (buChangeAutoRefresh.CheckState == CheckState.Checked);
        }

        private void cbInterval_TextChanged(object sender, EventArgs e)
        {
            int myInterval = 0;
            if (cbInterval.Text.Length > 0)
            {
                myInterval = Convert.ToInt32(cbInterval.Text);
                if (myInterval == 0)
                    myInterval = 500;
                timer1.Interval = 500;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (fRandomMode)
            {
                Random myRandomizer = new Random();
                int i = myRandomizer.Next(fWebCamList.WebCams.Count - 1);
                cbWebCamList.SelectedIndex = i;
            }
            RefreshPicture();
        }

        private void EhSelectWebCam(object sender, EventArgs e)
        {
            RefreshPicture();  
        }

        private void buRefresh_Click(object sender, EventArgs e)
        {
            RefreshPicture();  
        }

        private void buSwitchRandomMode_Click(object sender, EventArgs e)
        {
            fRandomMode = (buRandomMode.CheckState == CheckState.Checked);
            if (cbInterval.Text.Length > 0)
                timer1.Interval = Convert.ToInt32(cbInterval.Text);
            else
                timer1.Interval = 500;
            timer1.Enabled = fRandomMode;
        }

        private void buEditWebCamList_Click(object sender, EventArgs e)
        {
            FrmEditList myEditForm = new FrmEditList();
            myEditForm.Show();
        }


        
        private void Form1_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == WindowState)
                Hide();

        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;

        }



        public enum Style : int
        {
            Tiled, Centered, Stretched
        }


        private void setWallPaper()
        {
            string fFileName = fPictureManager.GetCurrentFileName(); ;
            //Wallpaper.Set(new Uri(fFileName), Wallpaper.Style.Stretched);
        }

        private void buSetWallpaper_Click(object sender, EventArgs e)
        {
            setWallPaper();	
        }



    }
}
